package biznussoft.metercor.fragments;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import biznussoft.metercor.R;
import biznussoft.metercor.listeners.GlobalListener;
import biznussoft.metercor.model.Bean_Class;
import biznussoft.metercor.model.ContactsModel;
import biznussoft.metercor.utils.ExpandCollapseAnimation;

public class Contacts2Fragment  extends Fragment{

	private GlobalListener mHomeItemListener;   
	private ImageView  iv_expandcollapse;
	private LinearLayout sectionitemsParent;
	private  LinearLayout mainLayout;
	private  TableLayout tableLayout ;
	 
	 Bean_Class model;
	 
	 ArrayList<ContactsModel> list;
	private int mHeight = 0;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.data,container, false);
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		mHomeItemListener = (GlobalListener) getActivity();
		super.onAttach(activity);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		 mainLayout = (LinearLayout) getView().findViewById(R.id.abc);

		TextView header = (TextView)getView().findViewById(R.id.tvHeader);
		header.setText("Contacts");

		TextView Name = (TextView) getView().findViewById(R.id.tvLastName);
		TextView Title = (TextView)  getView().findViewById(R.id.tvTitle);
		TextView Email = (TextView)  getView().findViewById(R.id.tvEmail);
		TextView Phone = (TextView)  getView().findViewById(R.id.tvPhone);

		Name.setText("LastName");
		Title.setText("Title");
		Email.setText("Email");
		Phone.setText("Phone");
		
		   iv_expandcollapse = (ImageView) getView().findViewById(R.id.iv_CollapseExpand);
		  sectionitemsParent = (LinearLayout) getView().findViewById(R.id.abc);
		  tableLayout = (TableLayout) getView().findViewById(R.id.ttlayout);
		
	

	}
	
	public void showContacts(int position){
		
		model = AccountsFragment.list.get(position);
		list = AccountsFragment.subContactsList.get(model.getId());
		

		for (int i = 0; i < list.size(); i++) {

			ContactsModel donor = list.get(i);
			View view = getActivity().getLayoutInflater().inflate(
					R.layout.fragment_list, mainLayout, false);

			final int j =i;
			TextView mName = (TextView) view
					.findViewById(R.id.tvContactLastName);
			TextView mTitle = (TextView) view.findViewById(R.id.tvContactTitle);
			TextView mEmail = (TextView) view.findViewById(R.id.tvContactEmail);
			TextView mPhone = (TextView) view.findViewById(R.id.tvContactPhone);

			mName.setText(donor.getLastName());
			mTitle.setText(donor.getTitle());
			mEmail.setText(donor.getEmail());
			mPhone.setText(donor.getPhone());

			mainLayout.addView(view);         
			
			TableLayout tLayout = (TableLayout) view.findViewById(R.id.llSectionItems);
			
			tLayout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
//					mHomeItemListener.onGetContacts(j,"accounts");
				}
			});
			
			iv_expandcollapse.setImageResource(R.drawable.expand);
			sectionitemsParent.setVisibility(View.GONE);
			tableLayout.setVisibility(View.GONE);
			
			iv_expandcollapse.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
						if (sectionitemsParent.getVisibility() == View.VISIBLE) {// Collapse
							ExpandCollapseAnimation collapseAnimation = new ExpandCollapseAnimation(sectionitemsParent,tableLayout, 1000, 1, iv_expandcollapse);
							mHeight = collapseAnimation.getHeight();
							iv_expandcollapse.startAnimation(collapseAnimation);
						} else {// Expand
							ExpandCollapseAnimation expandAnimation = new ExpandCollapseAnimation(sectionitemsParent,tableLayout, 1000, 0, iv_expandcollapse);
							if (mHeight == 0) {
								mHeight = getSectionitemsHeight();
							}
							expandAnimation.setHeight(mHeight);
							iv_expandcollapse.startAnimation(expandAnimation);
						}
					}

					private int getSectionitemsHeight() {
						return (int) (4 * getResources().getDimension(R.dimen.section_item_height));
				}
			});

		}
	}
	public static View getDividerView(Context context) {
		View dividerView = new View(context);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2);
		dividerView.setLayoutParams(lp);
		dividerView.setBackgroundResource(android.R.drawable.divider_horizontal_bright);
		return dividerView;
	}



}